#include "timer.h"
#include "gpio.h"
#include "btim.h"
#include "work.h"

#define PLUSE_DELAY_CNT		(1)

uint8_t ms_cnt,b_cnt,us125,ems_pulse=0, ems2_pulse = 0,ems_cnt1;
uint16_t fan_cnt,fan_cnt1,ems_cnt;
boolean_t ms_f,ms10_f,ms10_f1,ms10_f3,Fan_Error=0;
extern boolean_t beep_f,ems_f;
extern uint8_t FAN_Duty,Work_Mode,pwm_period;
extern uint16_t sub_period;
uint8_t pluse_change_delay = 0;

void delay_us(uint16_t us)
{
		uint32_t i;
		for(i=0;i<us*4;i++)
		;
}

void delay_ms(uint16_t ms)
{
		uint32_t i;
		for(i=0;i<ms;i++)
		delay_us(1000);
}

void App_Btimer_Init(void)
{
    stc_btim_init_t    stcInitCfg = {0};    
    
    //����GTIM��Ч��BTIM3/4/5��Ч
    SYSCTRL_FuncEnable(SYSCTRL_FUNC_GTIMCONFIG);

    ///< ʹ��BTIM3/4/5 ����ʱ��
    SYSCTRL_PeriphClkEnable(SYSCTRL_PERIRESET_GTIM);
    
    stcInitCfg.u32TaskMode  = BTIM_TASK_MODE_CONTINUOUS_COUNTER; //����ģʽ����������
    stcInitCfg.u32WorkMode  = BTIM_WORK_MODE_PCLK;               //����ģʽ:��ʱ��ģʽ������ʱ��Դ����PCLK
    stcInitCfg.u32Prescaler = BTIM_COUNTER_CLK_DIV128;          //�Լ���ʱ�ӽ���Ԥ��Ƶ
    stcInitCfg.u32ToggleEn  = BTIM_TOGGLE_DISABLE;               //TOG�����ʹ��
    //stcInitCfg.u32TriggerSource = BTIM_TRIGGER_SOURCE_ETR;     //����Դѡ��ʹ���ⲿETR������ΪGATE�ſ�
    //stcInitCfg.u32ExInputPolarity = BTIM_ETR_POLARITY_NORMAL;  //�ⲿ���뼫��ѡ�񣬼��Բ���ת
    stcInitCfg.u32AutoReloadVal = 46-1;      //37     //100us             //�Զ����ؼĴ�ARR��ֵ
    Btim_Init(BTIM3, &stcInitCfg);
    
    Btim_ClearFlag(BTIM3, BTIM_IT_CLR_UI); //�������жϱ�־λ
    Btim_EnableIT(BTIM3, BTIM_IT_UI);      //����BTIM3����ж�    
    EnableNvic(BTIM3_4_5_IRQn, IrqLevel0, TRUE); 
		
		Btim_Enable(BTIM3); 
}

void BTim3_IRQHandler(void)
{
    if(TRUE == Btim_IsActiveFlag(BTIM3, BTIM_IT_FLAG_UI))
    {
		us125++;
		if(us125 > 7){
			us125 = 0;
			ms_f = 1;
			ms_cnt++;
			//39
			if(ms_cnt>9){
				ms_cnt = 0;
				ms10_f = 1;
				ms10_f1 = 1;
				ms10_f3 = 1;
			}
		}
		// 蜂鸣器处理， 120us翻转-次==4khz
		if(beep_f){
			GPIO_Toggle(BUZZER_PORT, BUZZER_PIN);
		}

#if 0
		// ems 输出波形控制--（1次ems的输出）===================
		if(ems_pulse == 1){
			if(Work_Mode != 1){		// 模式1 ，关闭第1路ems
				// EMS1_PWM_N_OFF;   //现在是盆底肌
				// EMS1_PWM_P_ON;
			}
			if(Work_Mode != 2){     // 模式2 ，关闭第2路ems
				EMS2_PWM_N_OFF;
				EMS2_PWM_P_ON;
			}
			pluse_change_delay ++;
			if(pluse_change_delay > 1){
				ems_pulse = 2;
				pluse_change_delay = 0;
			}
			
		}
		else if(ems_pulse == 2){
			// EMS1_PWM_N_OFF;
			// EMS1_PWM_P_OFF;

			EMS2_PWM_N_OFF;
			EMS2_PWM_P_OFF;

			pluse_change_delay ++;
			if(pluse_change_delay > 1){
				ems_pulse = 3;
				pluse_change_delay = 0;
			}
		}
		else if(ems_pulse == 3){
			if(Work_Mode != 1){     // 模式1 ，关闭第1路ems
				// EMS1_PWM_P_OFF;
				// EMS1_PWM_N_ON;
			}
			if(Work_Mode != 2){
				EMS2_PWM_P_OFF;   // 模式2 ，关闭第2路ems
				EMS2_PWM_N_ON;
			}

			pluse_change_delay ++;
			if(pluse_change_delay > 1){
				ems_pulse = 4;
				pluse_change_delay = 0;
			}
		}
		else {
			// EMS1_PWM_N_OFF;
			// EMS1_PWM_P_OFF;

			EMS2_PWM_N_OFF;
			EMS2_PWM_P_OFF;
			ems_pulse = 0;
		}
		//----盆底肌--- 占空比要小些
		switch (ems2_pulse)
		{
			case 1:
				if(Work_Mode != 1){     // 模式1 ，关闭第1路ems
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_ON;
					ems2_pulse = 2;
				}
			break;
			case 2:
				EMS1_PWM_N_ON;
				EMS1_PWM_P_OFF;
				ems2_pulse = 3;
			break;
			case 3:
				EMS1_PWM_N_OFF;
				EMS1_PWM_P_OFF;
				ems2_pulse = 0;
			break;
			default:
				EMS1_PWM_N_OFF;
				EMS1_PWM_P_OFF;
				break;
		}
#endif
		switch(Work_Mode){

#if 0			
			case 1:{
				if(ems_pulse == 1){
					EMS1_PWM_N_OFF;   // 单路输出时，另外一路置高，防止ems电压串到该路，造成本路也有电流感
					EMS1_PWM_P_OFF;
					EMS2_PWM_N_OFF;
					EMS2_PWM_P_ON;

					pluse_change_delay ++;
					if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 2;
						pluse_change_delay = 0;
					}
					
				}
				else if(ems_pulse == 2){
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_OFF;
					EMS2_PWM_N_OFF;
					EMS2_PWM_P_OFF;

					pluse_change_delay ++;
					if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 3;
						pluse_change_delay = 0;
					}
				}
				else if(ems_pulse == 3){

					EMS1_PWM_P_OFF;
					EMS1_PWM_N_OFF;
					EMS2_PWM_P_OFF;   // 模式2 ，关闭第2路ems
					EMS2_PWM_N_ON;

					pluse_change_delay ++;
					if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 4;
						pluse_change_delay = 0;
					}
				}
				else {
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_OFF;

					EMS2_PWM_N_OFF;
					EMS2_PWM_P_OFF;
					ems_pulse = 0;
				}
			}
			break;
			
			case 2:{
			if(ems_pulse == 1){
					EMS2_PWM_N_OFF;  
					EMS2_PWM_P_OFF;
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_ON;

					pluse_change_delay ++;
					if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 2;
						pluse_change_delay = 0;
					}
					
				}
				else if(ems_pulse == 2){
					EMS2_PWM_N_OFF;
					EMS2_PWM_P_OFF;
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_OFF;

					pluse_change_delay ++;
					if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 3;
						pluse_change_delay = 0;
					}
				}
				else if(ems_pulse == 3){

					EMS2_PWM_P_OFF;
					EMS2_PWM_N_OFF;
					EMS1_PWM_P_OFF;   // 模式2 ，关闭第2路ems
					EMS1_PWM_N_ON;

					pluse_change_delay ++;
					if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 4;
						pluse_change_delay = 0;
					}
				}
				else {
					EMS2_PWM_N_OFF;
					EMS2_PWM_P_OFF;

					EMS1_PWM_N_OFF;
					EMS1_PWM_P_OFF;
					ems_pulse = 0;
				}

			}
			break;

#endif
#if 0
			case 1:
			case 2:
			case 3:{

				if(ems_pulse == 1){
					EMS2_PWM_N_OFF;  
					EMS2_PWM_P_ON;
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_ON;

					pluse_change_delay ++;
					if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 2;
						pluse_change_delay = 0;
					}
					
				}
				else if(ems_pulse == 2){
					EMS2_PWM_N_OFF;
					EMS2_PWM_P_OFF;
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_OFF;

					pluse_change_delay ++;
					if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 3;
						pluse_change_delay = 0;
					}
				}
				else if(ems_pulse == 3){

					EMS2_PWM_P_OFF;
					EMS2_PWM_N_ON;
					EMS1_PWM_P_OFF;   // 模式2 ，关闭第2路ems
					EMS1_PWM_N_ON;

					pluse_change_delay ++;
					if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 4;
						pluse_change_delay = 0;
					}
				}
				else {
					EMS2_PWM_N_OFF;
					EMS2_PWM_P_OFF;

					EMS1_PWM_N_OFF;
					EMS1_PWM_P_OFF;
					ems_pulse = 0;
				}
			}

			break;
#endif
#if 0
			case 1:
			case 2:
			case 3:
			case 4:{

				if(ems_pulse == 1){
					EMS2_PWM_N_OFF;  
					EMS2_PWM_P_ON;
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_ON;

					pluse_change_delay ++;
					// if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 2;
						pluse_change_delay = 0;
					// }
					
				}
				else if(ems_pulse == 2){
					EMS2_PWM_N_OFF;
					EMS2_PWM_P_OFF;
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_OFF;

					pluse_change_delay ++;
					// if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 3;
						pluse_change_delay = 0;
					// }
				}
				else if(ems_pulse == 3){

					EMS2_PWM_P_OFF;
					EMS2_PWM_N_ON;
					EMS1_PWM_P_OFF;   // 模式2 ，关闭第2路ems
					EMS1_PWM_N_ON;

					pluse_change_delay ++;
					// if(pluse_change_delay > PLUSE_DELAY_CNT){
						ems_pulse = 4;
						pluse_change_delay = 0;
					// }
				}
				else {
					EMS2_PWM_N_OFF;
					EMS2_PWM_P_OFF;

					EMS1_PWM_N_OFF;
					EMS1_PWM_P_OFF;
					ems_pulse = 0;
				}
			}
			break;
#endif

			case 1:
				if(ems_pulse == 1){
					if(pluse_change_delay%2){
						EMS2_PWM_N_OFF;  
						EMS2_PWM_P_ON;
						EMS1_PWM_N_OFF;
						EMS1_PWM_P_ON;
					}
					else{
						EMS2_PWM_P_OFF;  
						EMS2_PWM_N_ON;
						EMS1_PWM_P_OFF;
						EMS1_PWM_N_ON;
					}	
					ems_pulse = 2;
				}
				else if(ems_pulse == 2){
					if(pluse_change_delay%2){
						EMS2_PWM_P_OFF;
						EMS2_PWM_N_ON;
						EMS1_PWM_P_OFF;   // 模式2 ，关闭第2路ems
						EMS1_PWM_N_ON;
					}
					else{
						EMS2_PWM_N_OFF;
						EMS2_PWM_P_ON;
						EMS1_PWM_N_OFF;   // 模式2 ，关闭第2路ems
						EMS1_PWM_P_ON;	
					}
					ems_pulse = 3;
				}
				else {
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_OFF;
					EMS2_PWM_N_OFF;
					EMS2_PWM_P_OFF;
					
					ems_pulse = 0;
					pluse_change_delay ++;
				}
				break;

			case 2:
				if(ems_pulse == 1){
					if(pluse_change_delay%2){
						EMS2_PWM_N_OFF;  
						EMS2_PWM_P_ON;
						EMS1_PWM_N_OFF;
						EMS1_PWM_P_ON;
					}
					else{
						EMS2_PWM_P_OFF;  
						EMS2_PWM_N_ON;
						EMS1_PWM_P_OFF;
						EMS1_PWM_N_ON;
					}	
					ems_pulse = 2;
				}
				else if(ems_pulse == 2){
					if(pluse_change_delay%2){
						EMS2_PWM_P_OFF;
						EMS2_PWM_N_ON;
						EMS1_PWM_P_OFF;   // 模式2 ，关闭第2路ems
						EMS1_PWM_N_ON;
					}
					else{
						EMS2_PWM_N_OFF;
						EMS2_PWM_P_ON;
						EMS1_PWM_N_OFF;   // 模式2 ，关闭第2路ems
						EMS1_PWM_P_ON;	
					}
					ems_pulse = 3;
				}
				else {
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_OFF;
					EMS2_PWM_N_OFF;
					EMS2_PWM_P_OFF;
					
					ems_pulse = 0;
					pluse_change_delay ++;
				}
				break;
			case 3:
			case 4:{
				if(ems_pulse == 1){
					if(pluse_change_delay%2){
						EMS2_PWM_N_OFF;  
						EMS2_PWM_P_ON;
						EMS1_PWM_N_OFF;
						EMS1_PWM_P_ON;
					}
					else{
						EMS2_PWM_P_OFF;  
						EMS2_PWM_N_ON;
						EMS1_PWM_P_OFF;
						EMS1_PWM_N_ON;
					}	
					ems_pulse = 2;
				}
				else if(ems_pulse == 2){
					if(pluse_change_delay%2){
						EMS2_PWM_P_OFF;
						EMS2_PWM_N_ON;
						EMS1_PWM_P_OFF;   // 模式2 ，关闭第2路ems
						EMS1_PWM_N_ON;
					}
					else{
						EMS2_PWM_N_OFF;
						EMS2_PWM_P_ON;
						EMS1_PWM_N_OFF;   // 模式2 ，关闭第2路ems
						EMS1_PWM_P_ON;	
					}
					ems_pulse = 3;
				
				}
				else {
					EMS1_PWM_N_OFF;
					EMS1_PWM_P_OFF;
					EMS2_PWM_N_OFF;
					EMS2_PWM_P_OFF;
					
					ems_pulse = 0;
					pluse_change_delay ++;
				}
			}
			break;

			default:{
				EMS2_PWM_N_OFF;
				EMS2_PWM_P_OFF;

				EMS1_PWM_N_OFF;
				EMS1_PWM_P_OFF;
				ems_pulse = 0;
				ems_f = 0;
			}
			break;

		}


//====================================================
		
		if(ems_f){
			// ems 周期
			ems_cnt++;
			if(ems_cnt > sub_period){
				ems_pulse = 1;
				ems2_pulse = 1;
				ems_cnt = 0;
			}
			// ems 升压可控制 ---这种方式不太好-- 正常的话，应该是根据档位调节占空比，而不是开关整体
			ems_cnt1++;
			if(ems_cnt1 > 64){
				ems_cnt1 = 0;
				GPIO_PB04_AF_ATIM3_CH2B_SET();
				GPIO_PB01_AF_ATIM3_CH1B_SET();
			}
			else if(ems_cnt1 > pwm_period)
			{
				GPIO_PB04_AF_GPIO_SET();
				GPIO_PB01_AF_GPIO_SET();
			}
#if 0			
			// level_1 pwm 4
			switch(Work_Mode){
				case 1:
					if(ems_cnt1 > 25)
					{
						GPIO_PB04_AF_GPIO_SET();
						// GPIO_PB01_AF_GPIO_SET();
					}
					break;

				case 2:
					if(ems_cnt1 > 4)
					{
						// GPIO_PB04_AF_GPIO_SET();
						GPIO_PB01_AF_GPIO_SET();
					}
					break;
				default:
					break;
			}
#endif
		}
		Btim_ClearFlag(BTIM3, BTIM_IT_CLR_UI);//���GTIM������жϱ�־λ    
	}
}
//systick�жϺ���
void SysTick_IRQHandler(void)
{
   
}
